﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Xml;
namespace DemoGame1
{
    public class MapWithCells : VisibleGameEntity
    {
        List<MapLayer> mapLayer;

        public Rectangle[] collision;
        //tesst
        Texture2D colli;
        SpriteFont _spriteFont;

        public MapWithCells(ContentManager content, string strXML)
        {
            mapLayer = new List<MapLayer>();

            XmlDocument doc = new XmlDocument();
            doc.Load(strXML);
            XmlElement etopleft = (XmlElement)doc.SelectSingleNode("//topleft");
            Vector2 topleft = new Vector2();
            foreach (XmlElement elm in etopleft.ChildNodes)
            {
                if (elm.Name == "x")
                {
                    topleft.X = (float)Convert.ToInt16(elm.InnerText.Trim());
                }
                if (elm.Name == "y")
                {
                    topleft.Y = (float)Convert.ToInt16(elm.InnerText.Trim());
                }
            }
            XmlElement esize = (XmlElement)doc.SelectSingleNode("//size");
            Vector2 size = new Vector2();
            foreach (XmlElement elm in esize.ChildNodes)
            {
                if (elm.Name == "x")
                {
                    size.X = (float)Convert.ToInt16(elm.InnerText.Trim());
                }
                if (elm.Name == "y")
                {
                    size.Y = (float)Convert.ToInt16(elm.InnerText.Trim());
                }
            }
            //get topleft and size
            _TopLeft = topleft;
            _Size = size;
            //

            XmlElement layers = (XmlElement)doc.SelectSingleNode("//layers");
            foreach (XmlElement layer in layers.ChildNodes)
            {
                MapLayer templayer = new MapLayer(content, layer);
                mapLayer.Add(templayer);
            }

            colli = content.Load<Texture2D>("Map/collision");
            string strFont = "Menu/menuFont";
            _spriteFont = content.Load<SpriteFont>(strFont);

            XmlNodeList collisionlist = doc.SelectNodes("//collision");
            collision = new Rectangle[collisionlist.Count];
            for (int i = 0; i < collisionlist.Count; i++)
            {
                XmlElement nodeDetails = (XmlElement)collisionlist[i];
                int x = 0;
                int y = 0;
                int width = 0;
                int height = 0;
                foreach (XmlElement elm in nodeDetails.ChildNodes)
                {
                    if (elm.Name == "x")
                    {
                        x = Convert.ToInt16(elm.InnerText);
                    }
                    if (elm.Name == "y")
                    {
                        y = Convert.ToInt16(elm.InnerText);
                    }
                    if (elm.Name == "width")
                    {
                        width = Convert.ToInt16(elm.InnerText);
                    }
                    if (elm.Name == "height")
                    {
                        height = Convert.ToInt16(elm.InnerText);
                    }
                }
                Rectangle rec = new Rectangle(x, y, width, height);
                collision[i] = rec;
            }

        }
        public void Move(Vector2 delta)
        {
            //move background
            mapLayer[0].Move(delta / 2);
            //
            //move base
            mapLayer[1].Move(delta);
            //
            //move mask
            mapLayer[2].Move(delta);

            //for (int i = 0; i < nRows; i++)
            //    for (int j = 0; j < nCols; j++)
            //    {
            //        _cell[i, j].TopLeft -= delta;
            //    }

            for (int i = 0; i < collision.Length; i++)
            {
                Point p = collision[i].Location;
                p.X = p.X - (int)delta.X;
                p.Y = p.Y - (int)delta.Y;
                collision[i].Location = p;
            }
        }
        public override void Draw(SpriteBatch sb, GameTime gameTime)
        {
            if (Visible)
            {
                //for (int i = 0; i < mapLayer.Count; i++)
                //    mapLayer[i].Draw(sb, gameTime);
                mapLayer[0].Draw(sb, gameTime);
                mapLayer[1].Draw(sb, gameTime);


            }
        }

        public void DrawMask(SpriteBatch sb, GameTime gameTime)
        {
            if (Visible)
            {
                mapLayer[2].Draw(sb, gameTime);
            }
        }

        public void DrawCollision(SpriteBatch sb, GameTime gameTime)
        {
            if (Visible)
            {
                for (int i = 0; i < collision.Length; i++)
                {
                   sb.Draw(colli, collision[i], Color.White);
                    sb.DrawString(_spriteFont, "" + i, new Vector2((float)collision[i].Left, (float)collision[i].Top), Color.Black);
                }
            }
        }
    }
}
